/**
 * 用户状态controller层
 */
package com.bys.ots.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bys.ots.model.Email;
import com.bys.ots.model.User;
import com.bys.ots.pojo.Result;
import com.bys.ots.pojo.ResultEnum;
import com.bys.ots.pojo.ResultUtil;
import com.bys.ots.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author bairuihua
 * 2019/11/25
 */

@Api(value = "User Controller")
@RestController
@RequestMapping(value = "v1")
public class UserController
{
    @Resource
    private UserService userService;

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @ApiOperation(value = "Login")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true, defaultValue = "{rawData:\"\"}")
            })
    @PostMapping(value = "/login")
    public Result login(@RequestBody String rawData, HttpSession session)
    {
        logger.info("->User start to login the system, the login info is：" + rawData);
        return userService.login(rawData, session);
    }
    /**
     * 获取openId的方法
     * @param code code值转换openId
     * @return
     */
    @ApiOperation("CODE转换openId")
    @ApiImplicitParam(name = "code", value = "code", dataType = "String")
    @GetMapping("/getCode")
    @ResponseBody
    public Result getCode(@RequestParam String code){
        return userService.getCode(code);
    }

    @ApiOperation("获取调用凭据")
    @ApiImplicitParam()
    @GetMapping("/getToken")
    @ResponseBody
    public Result getToken(){
        return userService.getToken();
    }

    @ApiOperation(value = "loginTest")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true, defaultValue = "{email:\"\";checkCode:\"\"}")
            })
    @PostMapping(value = "/loginTest")
    public Result loginTest(@RequestBody String rawData, HttpSession session)
    {
        logger.info("->User start to login the system, the login info is：" + rawData);
        return userService.loginTest(rawData, session);
    }

    @ApiOperation(value = "register")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true, defaultValue = "{emailAddress:\"\";openId:\"\";checkCode:\"\"}")
            })
    @PostMapping(value = "/regist")
    public Result register(@RequestBody String rawData, HttpSession session)
    {
        logger.info("->User start to register the system, the register info is：" + rawData);
        return userService.register(rawData, session);
    }

    @ApiOperation(value = "add email")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true, defaultValue = "{email:\"\";remark:\"\"}")
            })
    @PostMapping(value = "/addEmail")
    public Result addEmail(@RequestBody String rawData)
    {
        logger.info("->User start to enter controller /addEmail, the request param  is：" + rawData);
        return userService.addEmail(rawData);
    }

    @ApiOperation(value = "Delete Email")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true, defaultValue = "{email:\"\"}")
            })
    @PostMapping(value = "/deleteEmail")
    public Result deleteEmail(@RequestBody String rawData)
    {
        logger.info(
                "->User start to enter controller /deleteEmail, the request info is：" + rawData);
        return userService.deleteEmail(rawData);
    }

    @ApiOperation(value = "Search Email List")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", value = "请求体", required = true, dataType = "String", defaultValue = "{userId:\"\"}")
            })
    @GetMapping(value = "/searchEmailList")
    public Result searchEmailList(@RequestParam String userId)
    {
        logger.info("->User start to enter controller /searchEmailList, the search param is：" +
                            userId);
        return userService.searchEmailList(userId);
    }

    @ApiOperation(value = "Find User Info ")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", value = "请求体", required = true, dataType = "String", defaultValue = "{userId:\"\"}")
            })
    
    @GetMapping(value = "/findUserInfo")
    public Result findUserInfo(@RequestParam String userId) {
    	logger.info("->User start to enter controller /findUserInfo, the request param  is：" + userId);
    	
    	 return userService.findUserInfo(userId);
    	
    }
    
    @ApiOperation(value = "Update User Info")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true,defaultValue = "{telephone:\"\";userName:\"\";address:\"\";remark:\"\"}")
            })
    
    @PostMapping(value = "/UpdateUserInfo")
    public Result UpdateUserInfo(@RequestBody User user) {
    	logger.info("->User start to enter controller /updateUserInfo, the request param  is：" + user.toString());
    
       return userService.updateUserInfo(user);
    
    }
    
    @ApiOperation(value = "find Email By Id ")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", value = "请求体", required = true, dataType = "String", defaultValue = "{Id:\"\"}")
            })
    
    @GetMapping(value = "/findEmailById")
    public Result findEmailById(@RequestParam String Id) {
    	logger.info("->User start to enter controller /findEmailById, the request param  is：" + Id);
    	
    	 return userService.findEmailById(Id);
    	
    }
    
    @ApiOperation(value = "Update Email")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", value = "请求体", required = true,defaultValue = "{email:\"\";remark:\"\";updateTime:\"\";id:\"\"}")
            })
    
    @PostMapping(value = "/UpdateEmail")
    public Result UpdateEmail(@RequestBody Email email) {
    	logger.info("->User start to enter controller /UpdateEmail, the request param  is：" + email.toString());
    
       return userService.UpdateEmail(email);
    
    }
    
    
    @GetMapping(value = "/helloword")
    public Result helloword()
    {
        return ResultUtil.OTSResult("Helloword");
    }

}
